//
//  CMDBConditionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/11.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let extraMarin : CGFloat  = 12

private let extraHeight : CGFloat = 2

class CMDBConditionView: CMDBBaseView {
    
    let showInfo = UIView.getInstance()
    
    var isAttributeLabel : Bool = false
    
    var dataArray : Array<AnyObject>? = [AnyObject]()
    
    var filterData : CMDBHomeWorksBean?

    var showAttributesArr : [CMDBWorksAttributeBean]?
    
    var showWorksLabelArr : [CMDBWorksBean]?
    
    var workLabelCount : Int = 0
    
    var workClass : Mirror!
    
    let KLineHeight : CGFloat = 45
    
    var selectedBtnArr = [CMDBFilterButton]()
    
    var conditionalArr = [String]()
    
    override func setUp() {
        super.setUp()
        
    }
    
    func updataDataSource(data : AnyObject?){
        
            filterData        = data as? CMDBHomeWorksBean
            
            let workClass     = Mirror(reflecting: filterData!)
            
            self.workClass    = workClass
            
            workLabelCount    = Int(workClass.children.count)
            
            showAttributesArr = filterData?.attributes
            
            showWorksLabelArr = filterData?.works
            
            buildSubViews()
    }
    
    override func buildSubViews() {
        
        buildWordLabelsView()
    }
}


extension CMDBConditionView{
    
    func buildWordLabelsView(){
        
        for i in 0 ..< workLabelCount{
            
            if i == 7 {
                continue
            }
            
            var tmpArr = [CMDBWorksLabelBean]()
            
            var tmpAttArr = [CMDBWorksAttributeBean]()
            
            if i == 0 {
                
                tmpAttArr = filterData!.attributes!
                
            }else if i == 1 {
                
                tmpArr = filterData!.types!
                
            }else if i == 2{
                
                tmpArr = filterData!.areas!
                
            }else if i == 3 {
                
                tmpArr = filterData!.years!
                
            }else if i == 4 {
                
                tmpArr = filterData!.durations!
                
            }else if i == 5 {
                
               tmpArr = filterData!.boxOffices!
                
            }else if i == 6 {
                
                tmpArr = filterData!.episodes!
                
            }
            
            
            let attributedView = UIScrollView.getInstance()
            
            attributedView.showsHorizontalScrollIndicator = false
            
            attributedView.showsVerticalScrollIndicator   = false
            
            attributedView.bounces                        = true
            
            attributedView.frame                          = CGRect(x: 0, y: CGFloat(i) * KLineHeight, width: CMDB_Screen_Width, height: KLineHeight)
            
            addSubview(attributedView)
            
            var previousLabelMaxX : CGFloat?
            
            if i == 0 {
                
                for (index,showAttrbute) in tmpAttArr.enumerate() {
                    
                    let allAttButton = CMDBFilterButton(title: showAttrbute.name, borderColor:UIColor.Hex(0xff6d00) , titleSizeFont: CMDB_16_FONT, isShowBorder: true)
                    
                        allAttButton.row = i
                    
                    if index == 0 {
                        
                        allAttButton.col = index
                        
                        attributedView.addSubview(allAttButton)
                        
                        allAttButton.frame = CGRect(x: 15, y: 9, width: allAttButton.width + extraMarin, height: allAttButton.height - extraHeight)
                        
                        allAttButton.selected = true
                        
                        previousLabelMaxX = CGRectGetMaxX(allAttButton.frame)
                        
                        allAttButton.addTarget(self, action: #selector(clicked(_:)), forControlEvents: .TouchUpInside)
                        
                        allAttButton.tag  = 0
                        
                        selectedBtnArr.append(allAttButton)
                        
                    }else{
                        
                        let attributeButton = CMDBFilterButton(title: showAttrbute.name, borderColor: UIColor.Hex(0xff6d00), titleSizeFont: CMDB_16_FONT, isShowBorder: false)
                        
                        attributeButton.row = i
                        
                        attributeButton.col = index
                        
                        attributedView.addSubview(attributeButton)
                        
                        attributeButton.frame = CGRect(x: previousLabelMaxX! + 15, y: 9, width: attributeButton.width + extraMarin, height: attributeButton.height - extraHeight)
                        
                        previousLabelMaxX = CGRectGetMaxX(attributeButton.frame)
                        
                        attributeButton.addTarget(self, action: #selector(clicked(_:)), forControlEvents: .TouchUpInside)
                        
                        attributedView.addSubview(attributeButton)
                        
                        attributeButton.selected = false
                        
                        attributeButton.tag = index
                        
                    }
                }

            }else{
                for (index,tempArrBtnLabel) in tmpArr.enumerate() {
                    
                    let allAttButton = CMDBFilterButton(title: tempArrBtnLabel.label, borderColor:UIColor.Hex(0xff6d00) , titleSizeFont: CMDB_16_FONT, isShowBorder: true)
                    
                    
                    allAttButton.row = i
                    
                    if index == 0 {
                        
                        allAttButton.col = index
                        
                        attributedView.addSubview(allAttButton)
                        
                        allAttButton.frame = CGRect(x: 15, y: 9, width: allAttButton.width + extraMarin, height: allAttButton.height - extraHeight)
                        
                        allAttButton.selected = true
                        
                        previousLabelMaxX = CGRectGetMaxX(allAttButton.frame)
                        
                        allAttButton.addTarget(self, action: #selector(clicked(_:)), forControlEvents: .TouchUpInside)
                        
                        allAttButton.tag  = 0
                        
                        selectedBtnArr.append(allAttButton)
                        
                    }else{
                        
                        
                        let attributeButton = CMDBFilterButton(title: tempArrBtnLabel.label, borderColor: UIColor.Hex(0xff6d00), titleSizeFont: CMDB_16_FONT, isShowBorder: false)
                        
                        attributeButton.row = i
                        
                        attributeButton.col = index
                        
                        attributedView.addSubview(attributeButton)
                        
                        attributeButton.frame = CGRect(x: previousLabelMaxX! + 15, y: 9, width: attributeButton.width + extraMarin, height: attributeButton.height - extraHeight)
                        
                        previousLabelMaxX = CGRectGetMaxX(attributeButton.frame)
                        
                        attributeButton.addTarget(self, action: #selector(clicked(_:)), forControlEvents: .TouchUpInside)
                        
                        attributedView.addSubview(attributeButton)
                        
                        attributeButton.selected = false
                        
                        attributeButton.tag = index
                    }
                }
            }

             attributedView.contentSize = CGSize(width: previousLabelMaxX!, height: 0)
            
        }
        
        _ = selectedBtnArr.map {
//            printLog($0.col)
//            printLog($0.row)
//            printLog($0.titleLabel!.text)
            conditionalArr.append($0.titleLabel!.text!)
        }
    }
    
}

extension CMDBConditionView{
    
    @objc func clicked(clickedBtn : CMDBFilterButton){
        
        let cilckCoordinateBtn = clickedBtn
        
        let thatRowSelectedBtn = selectedBtnArr[cilckCoordinateBtn.row]
        
        thatRowSelectedBtn.selected = !thatRowSelectedBtn.selected
        
        thatRowSelectedBtn.layer.borderWidth = 0
        
        cilckCoordinateBtn.layer.borderWidth         = 0.5
        
        cilckCoordinateBtn.selected = !clickedBtn.selected
        
        cilckCoordinateBtn.setTitleColor(UIColor.Hex(0xff6d00), forState: .Selected)
        
        selectedBtnArr.replaceRange(Range(cilckCoordinateBtn.row..<cilckCoordinateBtn.row + 1), with: [cilckCoordinateBtn])
        
        
        
        conditionalArr.replaceRange(Range(cilckCoordinateBtn.row..<cilckCoordinateBtn.row + 1), with: [cilckCoordinateBtn.titleLabel!.text!])
        
        _ = conditionalArr.map {
            printLog($0)
        }
        
//        printLog("row======\(clickedBtn.row)")
//        
//        printLog("col======\(clickedBtn.col)")
        
        mDelegate?.onItemClickType!("DataBaseConditionBtnClick", data: clickedBtn, indexPath: nil)
    }
    
}
